import Mock from 'mockjs'
import {getParameter} from '@/common/js/util'
// 获取 mock.Random 对象
const Random = Mock.Random;

// mock一组数据
const loginData = function (info) {
  let msg = {state: 0, msg: '账号或密码错误！'};
  let userInfo = {account:'yuzx',pwd:'123456',token:'token123'};
  let user = JSON.parse(info.body).params;
  if (userInfo.account === user.account && userInfo.pwd === user.pwd) {
    msg.state = 1;
    msg.msg = '登录成功！';
    msg.user = userInfo;
  }
  return msg;
}
let kpiLists = [{name:"无敌",count_date:"2018-05",recharge_month:500,project:'300'},{name:"江湖",count_date:"2018-05",recharge_month:700,project:'100'}];
const kpi = function (info) {
  let msg = {};
  let obj = JSON.parse(info.body).params;
  msg.state = 1;
  msg.msg = '添加成功！';
  msg.obj = obj;
  msg.type = "success";
  kpiLists.push(obj);
  return msg;
}
const updatekpi = function (info) {
  let msg = {};
  let obj = JSON.parse(info.body).params;
 kpiLists.find((item)=>item.name===obj.name&&item.count_date===obj.count_date).recharge_month = obj.recharge_month;
  msg.state = 1;
  msg.msg = '修改成功！';
  msg.obj = obj;
  msg.type = "success";
  return msg;
}
const delkpi = function (info) {
  let msg = {};
  let obj = getParameter(info.url);
  kpiLists = kpiLists.filter(item=>item.project!==obj.project||item.count_date!==obj.count_date);
  msg.state = 1;
  msg.msg = '删除成功！';
  msg.obj = obj;
  msg.type = "success";
  return msg;
}
// Mock.mock( url, post/get , 返回的数据)；
Mock.mock('/loginData', 'post', loginData);
Mock.mock(/\/schedule\/kpi/, 'post', kpi);
Mock.mock(/\/schedule\/kpi/, 'get', (param)=> {
  param = getParameter(param.url);
  return param.project?kpiLists.filter((item)=>item.project === param.project):kpiLists;
});
Mock.mock(/\/schedule\/kpi/, 'put', updatekpi);
Mock.mock(/\/schedule\/kpi/, 'delete', delkpi);
